<?php
// this is another example showing how the dPFramework is working
// additionally we will have an easy database connection here

// as we are now within the tab box, we have to state (call) the needed information saved in the variables of the parent function
GLOBAL $AppUI, $canRead, $canEdit, $m, $tab;
//$tab = intval( dPgetParam( $_GET, 'tab', 0 ) );
require_once(DP_BASE_DIR."/modules/masterdata/mdcostcenter.class.php");
require_once(DP_BASE_DIR."/modules/masterdata/mddivision.class.php");
if (!$canRead) {			// lock out users that do not have at least readPermission on this module
	$AppUI->redirect( "m=public&a=access_denied" );
}
$active = intval( dPgetParam( $_REQUEST, 'active', 1 ) );
$division =  $_REQUEST['division'];
$division_list = arrayMerge( array(''=>$AppUI->_('--Select Division--')), getDivisionList());
//prepare an html table with a head section
?>
<table class="infopanel" width="100%" cellspacing="0" cellpadding="7" border="0">
<tr>
	<td colspan="100%"><input type="button" class="button" onClick="doEditCostCenter(0)" value="<?php echo $AppUI->_( 'New cost center' );?>"/></td>
	<td align="right">
		<form name="MdCostCenterFilter" action="?m=<?=$m?>&tab=<?=$tab?>" method="post">
			<?= arraySelect($division_list,'division','style="font-size:10px" onchange="this.form.submit()"',$division)?>
			<input class="button" onclick ="this.form.submit()" type="radio" name="active" value="-1" <?php if ($active == -1) echo "checked" ?> /><?=$AppUI->_('All')?>
			<input class="button" onclick ="this.form.submit()" type="radio" name="active" value="1" <?php if ($active == 1) echo "checked" ?> /> <?=$AppUI->_('Active') ?>
			<input class="button" onclick ="this.form.submit()" type="radio" name="active" value="0" <?php if ($active == 0) echo "checked" ?> /> <?=$AppUI->_('Disable')?>
		</form>
	</td>
</tr>
</table>
<form name="MdCostCenterDelete" id="MdCostCenterDelete" method="post">
	<input type="hidden" name="dosql" value ="mdcostcenter_do_aed" />
	<input type="hidden" name="m" value ="masterdata" />
	<input type="hidden" id="del_cc_id" name="cc_id" value ="" />
	<input type="hidden" name="del" value ="" />
</form>
<table id="idTableMasterdata" class="list" width="100%" cellspacing="0" cellpadding="2" border="0">
<tbody>
<tr>
	<th nowrap="nowrap">&nbsp;</th>
	<!--th nowrap="nowrap"><?php echo $AppUI->_( 'Code' );?></th-->
	<th nowrap="nowrap"><?php echo $AppUI->_( 'Name' );?></th>
	<th nowrap="nowrap"><?php echo $AppUI->_( 'Division' );?></th>
	<th nowrap="nowrap"><?php echo $AppUI->_( 'Active' );?></th>
	<th nowrap="nowrap">&nbsp;</th>
</tr>

<?php
// retrieving some dynamic content using an easy database query
/*
$costcenterO = new CMdCostCenter();
$costcenter = $costcenterO->loadAllCostCenter($active);
*/
$q = new DBQuery;
$q->addTable('costcenters');
$q->addJoin('divisions', '', 'div_id = cc_division');
$q->addQuery('costcenters.*, div_name');
if($active >-1){
	$q->addWhere('cc_active = '.$active);
}
if($division!= null && $division!=''){
	$q->addWhere('cc_division= '.$division);
}
$q->addOrder('cc_name');
$costcenter	= $q->loadList();

// add/show now gradually the einstein quotes
if (empty($costcenter)) {?>
<tr class="second">
<td colspan="100"><?php echo $AppUI->_( 'No item available' );?></td>
</tr>
<?php
}
$i = 0;
foreach ($costcenter as $row) {		//parse the array of einstein quotes
	$i++;
?>
<tr class="<?=($i % 2 == 0) ? "second" : "first" ?>">
	<td nowrap="nowrap" width="20">


	<?php if ($canEdit) {	// in case of writePermission on the module show an icon providing edit functionality for the given quote item

		// call the edit site with the unique id of the quote item

		// use the dPFrameWork to show the image
		// always use this way via the framework instead of hard code for the advantage
		// of central improvement of code in case of bugs etc. and for other reasons
		//echo "\n".'<a href="./index.php?m=masterdata&a=mdcostcenter_edit&costcenter_id=' . $row["costcenter_id"] . '">Test</a>';
		echo "\n".'<a onclick="doEditCostCenter('.$row["cc_id"]. ')">';
		echo dPshowImage( 'images/edit.gif' );
		echo "\n</a>";
		echo "&nbsp;";
	}
	?>
	</td>
	<!--td ><?php echo $row["cc_code"];?></td-->
	<td ><?php echo $row["cc_name"];?></td>
	<td ><?php echo $row["div_name"];?></td>
	<td ><?php if($row["cc_active"]==1){ echo dPshowImage( 'images/tick.gif' );}?></td>
	<td>
	<?php if ($canEdit) {
		showIconLink('images/delete.gif', 'js:doDeleteCostCenter('.$row["cc_id"]. ', \''.$row["cc_name"].'\')', 'Edit');
	}
	?>
	</td>
</tr>
<?php
}
?>
</tbody>
</table>
<div id="idCostCenterDialog" class="popup" style="visibility: hidden; left: 436px; top: 94.5px; display: none;">
</div>
<script type="text/javascript" language="javascript">
	Table("idTableMasterdata").setNormalHover(1);
	function doDeleteCostCenter(id, name)
	{
		if (confirm( "Do you want to delete "+ name)) {
			$('del_cc_id').value = id;
			f = $('MdCostCenterDelete');
			f.del.value = id;
			f.submit();
		}
	}
</script>